using System;
using Metro.SmartClient.LindFinancialFoundation.Constants;
using LINDFINANCIAL_API_COMPONENTLib;

namespace Metro.SmartClient.LindFinancialFoundation
{
    public class LindClass
    {
        private const string Firm = "532";
        private const string Account = "LAT5321";
        private const string Password = "1234";
        private string statusRecord;
        private string FillRecord;
        private string PositionRecord;
        private string FundsRecord;
        private string connectionStatus;
        private string InterfaceTypeLabel;
        private string RecordTypeLabel;
        public string quotes = String.Empty;  
        public int orderNumber = 0;
        public string orderIdentifier = String.Empty;  

        public string ConnectionStatus
        {
            get { return this.connectionStatus; }
            set { this.connectionStatus = value; }
        }

        public string StatusRecord
        {
            get { return this.statusRecord; }
            set { this.statusRecord = value; }
        }

      

        public LINDFINANCIAL_API_COMPONENTLib.RequestType eRequestType;

        protected string DisconnectReason;
        protected LINDFINANCIAL_API_COMPONENTLib.InterfaceType interfaceWaiting;
        protected LINDFINANCIAL_API_COMPONENTLib.recordType recordWaiting;

        protected int recordCount;
        public LINDFINANCIAL_API_COMPONENTLib.IConnect iConnect;
        public LINDFINANCIAL_API_COMPONENTLib.IOrder iOrder;
        public LINDFINANCIAL_API_COMPONENTLib.ILeg iLeg;
        public LINDFINANCIAL_API_COMPONENTLib.ICancel iCancel;
        public LINDFINANCIAL_API_COMPONENTLib.ICheck iCheck;
        public LINDFINANCIAL_API_COMPONENTLib.IRequest iRequest;
        public LINDFINANCIAL_API_COMPONENTLib.IStatus iStatus;
        public LINDFINANCIAL_API_COMPONENTLib.IAlert iAlert;
        public LINDFINANCIAL_API_COMPONENTLib.IFill iFill;
        public LINDFINANCIAL_API_COMPONENTLib.IPosition iPosition;
        public LINDFINANCIAL_API_COMPONENTLib.IFund iFund;
        public LINDFINANCIAL_API_COMPONENTLib.IBeginData iBeginData;
        public LINDFINANCIAL_API_COMPONENTLib.IEndData iEndData;
        public LINDFINANCIAL_API_COMPONENTLib.IHeartbeat iHeartbeat;
        public LINDFINANCIAL_API_COMPONENTLib.IQuote iQuote;
        public LINDFINANCIAL_API_COMPONENTLib.ISystem iSystem;
        public LINDFINANCIAL_API_COMPONENTLib.IContract iContract;
        public LINDFINANCIAL_API_COMPONENTLib.IPriceFormat iPriceformat;
        public LINDFINANCIAL_API_COMPONENTLib.IAccount iAccount;
        public LINDFINANCIAL_API_COMPONENTLib.IMap iMap;
        public LINDFINANCIAL_API_COMPONENTLib.IPackage iPackage;
        public LINDFINANCIAL_API_COMPONENTLib.ISub iSub;
        public LINDFINANCIAL_API_COMPONENTLib.IRisk iRisk;

        protected LINDFINANCIAL_API_COMPONENTLib.IConnectEvents iConnectEvents;
        protected LINDFINANCIAL_API_COMPONENTLib.IConnectEvents_Event iConnectEvents_Event;

        protected LINDFINANCIAL_API_COMPONENTLib.DisconnectReason tDisconnectReason;
        protected LINDFINANCIAL_API_COMPONENTLib.InterfaceType tInterfaceType;
        protected LINDFINANCIAL_API_COMPONENTLib.recordType tRecordType;

        public LINDFINANCIAL_API_COMPONENTLib.LindFinancial LindFinancial;


        public LindClass()
        {

            this.connectionStatus = WorkspaceNames.StoppedConnectionStatus;
            recordCount = 0;
        }

        public void StartConnection()
        {

            // Start LindFinancial API session

            LindFinancial = new LINDFINANCIAL_API_COMPONENTLib.LindFinancial();

            iConnect = (IConnect)LindFinancial;
            iStatus = (IStatus)LindFinancial;
            iOrder = (IOrder)LindFinancial;
            iLeg = (ILeg)LindFinancial;
            iCancel = (ICancel)LindFinancial;
            iCheck = (ICheck)LindFinancial;
            iFill = (IFill)LindFinancial;
            iAlert = (IAlert)LindFinancial;
            iPosition = (IPosition)LindFinancial;
            iFund = (IFund)LindFinancial;
            iBeginData = (IBeginData)LindFinancial;
            iEndData = (IEndData)LindFinancial;
            iRequest = (IRequest)LindFinancial;
            iHeartbeat = (IHeartbeat)LindFinancial;
            iQuote = (IQuote)LindFinancial;
            iSystem = (ISystem)LindFinancial;
            iContract = (IContract)LindFinancial;
            iPriceformat = (IPriceFormat)LindFinancial;
            iAccount = (IAccount)LindFinancial;
            iMap = (IMap)LindFinancial;
            iPackage = (IPackage)LindFinancial;
            iSub = (ISub)LindFinancial;
            iRisk = (IRisk)LindFinancial;


            if (this.connectionStatus == WorkspaceNames.StoppedConnectionStatus)
            {

                // Register interest in the COM event

                IConnectEvents_ConnectedEventHandler eConnected = new IConnectEvents_ConnectedEventHandler(Connected);
                IConnectEvents_DisconnectedEventHandler eDisconnected = new IConnectEvents_DisconnectedEventHandler(Disconnected);
                IConnectEvents_RecordAvailableEventHandler eRecordAvailable = new IConnectEvents_RecordAvailableEventHandler(RecordAvailable);

                LindFinancial.Connected += eConnected;
                LindFinancial.Disconnected += eDisconnected;
                LindFinancial.RecordAvailable += eRecordAvailable;

                iConnect.SetDefaults();

                iConnect.logName = "LindFinancialsNet" + Guid.NewGuid().ToString();
                iConnect.extendLog = true;
                iConnect.OpenLog();

                iConnect.fifoName = "";
                iConnect.host = "ptrade01.refco.com";
                iConnect.testNumber = 3;
                iConnect.firm = Firm;
                iConnect.account = Account;
                iConnect.password = Password;
                //iConnect.encryption = true;
                //iConnect.sign = true;
                iConnect.Start();

                this.connectionStatus = WorkspaceNames.StartedConnectionStatus;

                // Retrieve and delete any left over records from previous executions

                interfaceWaiting = iStatus.InterfaceType;

                if (interfaceWaiting != LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_NONE)
                {

                    iConnect.WriteLog("Retrieving leftover records");
                    ProcessRecords();
                }
            }

            if (this.connectionStatus == WorkspaceNames.StartedConnectionStatus || this.connectionStatus == WorkspaceNames.DisconnectedConnectionStatus)
            {

                DisconnectReason = "";
                this.connectionStatus = WorkspaceNames.ConnectingConnectionStatus;
                iConnect.Connect();
            }

            //Block on connection
            while (this.connectionStatus == WorkspaceNames.ConnectingConnectionStatus) ;
        }

        public void EndConnection()
        {

            if (this.connectionStatus == WorkspaceNames.ConnectedConnectionStatus || this.connectionStatus == WorkspaceNames.ConnectingConnectionStatus)
            {

                this.connectionStatus = WorkspaceNames.DisconnectedConnectionStatus;
                iConnect.CloseLog();
                iConnect.Disconnect();
                iConnect.Stop();
            }

            this.connectionStatus = WorkspaceNames.StoppedConnectionStatus;
        }


        //Connection Callback

        public void Connected()
        {

            this.connectionStatus = WorkspaceNames.ConnectedConnectionStatus;
            iConnect.WriteLog(this.connectionStatus);

        }

        //RecordAvailable Callback

        public void RecordAvailable()
        {

            iConnect.WriteLog("@@LINDFINANCIALCOMP.NET@@@@@@@@@@@@@@@@@@@@ RecordAvailable ");

            if (this.connectionStatus != WorkspaceNames.StoppedConnectionStatus)
            {
                ProcessRecords();
            }
        }

        // Disconnected Callback

        public void Disconnected(LINDFINANCIAL_API_COMPONENTLib.DisconnectReason reason)
        {

            if (this.connectionStatus != WorkspaceNames.StoppedConnectionStatus)
            {

                this.connectionStatus = WorkspaceNames.DisconnectedConnectionStatus;
            }

            switch (reason)
            {

                case LINDFINANCIAL_API_COMPONENTLib.DisconnectReason.DCR_NEVER_CONNECTED:
                    DisconnectReason = "Never Connected";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.DisconnectReason.DCR_LAUNCHER_DOWN:
                    DisconnectReason = "Launcher Down";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.DisconnectReason.DCR_INVALID_LOGON:
                    DisconnectReason = "Invalid Logon";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.DisconnectReason.DCR_ALREADY_CONNECTED:
                    DisconnectReason = "Already Connected Elsewhere";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.DisconnectReason.DCR_NO_LOGON_RESPONSE:
                    DisconnectReason = "No logon Response";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.DisconnectReason.DCR_CONNECTION_ERROR:
                    DisconnectReason = "Connection Error";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.DisconnectReason.DCR_SOCKET_ERROR:
                    DisconnectReason = "Socket Error";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.DisconnectReason.DCR_SOCKET_HANGUP:
                    DisconnectReason = "Expected Disconnect";
                    break;
            }

            this.connectionStatus += " - " + DisconnectReason;
            iConnect.WriteLog(this.connectionStatus);

        }


        public void ProcessRecords()
        {

            while (iStatus.InterfaceType != LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_NONE && (this.connectionStatus == WorkspaceNames.ConnectedConnectionStatus || this.connectionStatus == WorkspaceNames.StartedConnectionStatus))
            {
                switch (iStatus.InterfaceType)
                {
                    case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_STATUS:
                        recordCount++;

                        // Fetch the record from the fifo file
                        iStatus.Fetch();
                        DisplayTypeofCurrentRecord();

                        // Load Buffer Record
                        this.statusRecord += "* StatusRecord " + recordCount.ToString() + " {" + iStatus.status + "}";
                        orderNumber = iStatus.orderNumber;
                        orderIdentifier = iStatus.identifier.Trim();  

                        // Delete the record from the fifo file
                        iStatus.Finished();
                        break;

                    case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_ALERT:
                        recordCount++;

                        // Fetch the record from the fifo file
                        iAlert.Fetch();
                        DisplayTypeofCurrentRecord();

                        // Delete the record from the fifo file
                        iAlert.Finished();
                        break;

                    case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_FILL:
                        recordCount++;

                        // Fetch the record from the fifo file
                        iFill.Fetch();
                        DisplayTypeofCurrentRecord();

                        // Load Buffer Record
                        FillRecord += "* FillRecord " + recordCount.ToString() + " {" + iFill.fill + "}";

                        //Delete the record from the fifo file
                        iFill.Finished();
                        break;

                    case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_POSITION:
                        recordCount++;

                        // Fetch the record from the fifo file
                        iPosition.Fetch();
                        DisplayTypeofCurrentRecord();

                        // Load Buffer Record
                        PositionRecord += "* PositionRecord " + recordCount.ToString() + " {" + iPosition.position + "}";

                        // Delete the record from the fifo file
                        iPosition.Finished();
                        break;

                    case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_FUND:
                        recordCount++;

                        // Fetch the record from the fifo file
                        iFund.Fetch();
                        DisplayTypeofCurrentRecord();

                        // Load Buffer Record
                        FundsRecord += "* FundsRecord " + recordCount.ToString() + " {" + iFund.fund + "}";

                        // Write exchange rate to Log
                        iConnect.WriteLog("Fund Exchange Rate " + iFund.exchangeRate.ToString());

                        // Delete the record from the fifo file
                        iFund.Finished();
                        break;

                    case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_BEGIN_DATA:
                        recordCount++;

                        // Fetch the record from the fifo file
                        iBeginData.Fetch();
                        DisplayTypeofCurrentRecord();

                        // Delete the record from the fifo file
                        iBeginData.Finished();
                        break;

                    case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_END_DATA:
                        recordCount++;

                        // Fetch the record from the fifo file
                        iEndData.Fetch();
                        DisplayTypeofCurrentRecord();

                        // Delete the record from the fifo file
                        iEndData.Finished();
                        break;

                    case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_HEARTBEAT:
                        recordCount++;

                        // Fetch the record from the fifo file
                        iHeartbeat.Fetch();
                        DisplayTypeofCurrentRecord();

                        // Delete the record from the fifo file
                        iHeartbeat.Finished();
                        break;
                    case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_QUOTE:
                        recordCount++;
                        iQuote.Fetch();
                        this.quotes += "* Quotes " + recordCount.ToString() + " {" + iQuote.quote + "}";
                        iQuote.Finished();  
                        break;
                    default:
                        iConnect.Fetch();
                        iConnect.Released();
                        break;
                }
            }
        }


        public void DisplayTypeofCurrentRecord()
        {

            recordWaiting = LINDFINANCIAL_API_COMPONENTLib.recordType.RT_NONE;

            switch (iStatus.InterfaceType)
            {
                case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_NONE:
                    InterfaceTypeLabel = "None";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_STATUS:
                    InterfaceTypeLabel = "Status";
                    recordWaiting = iStatus.recordType;
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_ALERT:
                    InterfaceTypeLabel = "Alert";
                    recordWaiting = iAlert.recordType;
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_FILL:
                    InterfaceTypeLabel = "Fill";
                    recordWaiting = iFill.recordType;
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_POSITION:
                    InterfaceTypeLabel = "Position";
                    recordWaiting = iPosition.recordType;
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_FUND:
                    InterfaceTypeLabel = "Fund";
                    recordWaiting = iFund.recordType;
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_BEGIN_DATA:
                    InterfaceTypeLabel = "BeginData";
                    recordWaiting = iBeginData.recordType;
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_END_DATA:
                    InterfaceTypeLabel = "EndData";
                    recordWaiting = iEndData.recordType;
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_HEARTBEAT:
                    InterfaceTypeLabel = "Heartbeat";
                    recordWaiting = iHeartbeat.recordType;
                    break;
            }

            iConnect.WriteLog("@@LINDFINANCIALCOMP.NET@@@@@@@@@@@@@@@@@@@@ InterfaceType {" + InterfaceTypeLabel + "}");

            switch (recordWaiting)
            {

                case LINDFINANCIAL_API_COMPONENTLib.recordType.RT_NONE:
                    RecordTypeLabel = "None";
                    interfaceWaiting = LINDFINANCIAL_API_COMPONENTLib.InterfaceType.IT_NONE;
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.recordType.RT_STATUS:
                    RecordTypeLabel = "Status";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.recordType.RT_RSTATUS:
                    RecordTypeLabel = "RequestStatus";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.recordType.RT_TSTATUS:
                    RecordTypeLabel = "TestStatus";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.recordType.RT_RCHECK:
                    RecordTypeLabel = "Check";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.recordType.RT_ALERT:
                    RecordTypeLabel = "Alert";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.recordType.RT_FILL:
                    RecordTypeLabel = "Fill";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.recordType.RT_RFILL:
                    RecordTypeLabel = "RequestFill";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.recordType.RT_POSITION:
                    RecordTypeLabel = "Position";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.recordType.RT_FUND:
                    RecordTypeLabel = "Fund";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.recordType.RT_BEGIN_DATA:
                    RecordTypeLabel = "Begin Data";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.recordType.RT_END_DATA:
                    RecordTypeLabel = "End Data";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.recordType.RT_TIMEOUT:
                    RecordTypeLabel = "Timeout";
                    break;
                case LINDFINANCIAL_API_COMPONENTLib.recordType.RT_ECHO:
                    RecordTypeLabel = "Echo";
                    break;
            }
        }

        public void OnRequestPositions(Object sender, EventArgs e)
        {

            iConnect.WriteLog("Positions Requested");
            if (this.connectionStatus != WorkspaceNames.StoppedConnectionStatus)
            {
                SendRequest(LINDFINANCIAL_API_COMPONENTLib.RequestType.RQT_POSITIONS);
            }
        }

        public void OnRequestFunds(Object sender, EventArgs e)
        {

            iConnect.WriteLog("Funds Requested");
            if (this.connectionStatus != WorkspaceNames.StoppedConnectionStatus)
            {
                SendRequest(LINDFINANCIAL_API_COMPONENTLib.RequestType.RQT_FUNDS);
            }
        }

        public void OnRequestFills(Object sender, EventArgs e)
        {

            iConnect.WriteLog("Fills Requested");
            if (this.connectionStatus != WorkspaceNames.StoppedConnectionStatus)
            {
                SendRequest(LINDFINANCIAL_API_COMPONENTLib.RequestType.RQT_FILLS);
            }
        }

        public void OnRequestOrders(Object sender, EventArgs e)
        {

            iConnect.WriteLog("Orders Requested");
            if (this.connectionStatus != WorkspaceNames.StoppedConnectionStatus)
            {
                SendRequest(LINDFINANCIAL_API_COMPONENTLib.RequestType.RQT_ORDERS);
            }
        }

        public void SendRequest(LINDFINANCIAL_API_COMPONENTLib.RequestType rtype)
        {
            
            // Prepare request
            recordCount = 0;
            this.statusRecord = "";
            FillRecord = "";
            PositionRecord = "";
            FundsRecord = "";

            iRequest.request = "";
            iRequest.RequestType = rtype;
            iRequest.recordType = LINDFINANCIAL_API_COMPONENTLib.recordType.RT_REQUEST;
            iRequest.firm = Firm;
            iRequest.account = Account;
            iRequest.requestIdentifier = this.IdGeneration().ToString(); 

            //Log the record and send it
            iConnect.WriteLog(iRequest.request);

            //Initiate request and exit
            iRequest.Send();
        }

        public int IdGeneration()
        {
            Random rand = new Random();
            int id= 100000 * rand.Next(1, 1000) + 1;
            return id; 
        }

        public void GetFundInformation()
        {
            try
            {
              

                iRequest.request = "";
                iRequest.firm = Firm;
                iRequest.account = Account;
                iRequest.recordType = LINDFINANCIAL_API_COMPONENTLib.recordType.RT_REQUEST;
                iRequest.RequestType = LINDFINANCIAL_API_COMPONENTLib.RequestType.RQT_GETACCOUNT_VERBOSE;
                iRequest.requestIdentifier = this.IdGeneration().ToString();
                iRequest.Send();
                iRequest.RequestType = LINDFINANCIAL_API_COMPONENTLib.RequestType.RQT_FUNDS;
                iRequest.requestIdentifier = this.IdGeneration().ToString();
                iRequest.Send();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void QuoteInformation(string quote, DateTime date)
        {
            try
            {
                iRequest.request = "";
                iRequest.set_numberOfQuotes(QuoteIndex.QUOTE_1);
                iRequest.firm = Firm;
                iRequest.account = Account;
                iRequest.requestIdentifier = this.IdGeneration().ToString();
                iRequest.RequestType = LINDFINANCIAL_API_COMPONENTLib.RequestType.RQT_QUOTE;
                iRequest.commodityCode = quote;
                iRequest.month = OrderSharedServices.GetMonth(date);
                iRequest.set_year(OrderSharedServices.GetYear(date));
                //iRequest.putOrCall = LINDFINANCIAL_API_COMPONENTLib.OptionType.OPT_CALL;
                //iRequest.set_strikePrice(price);
                iRequest.addQuote(QuoteIndex.QUOTE_1, iRequest.buildQuote());
                iRequest.Send();

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }



        public void PutOrder(int quantity, int price, DateTime date, TransactionType transactionType, string commodityCode,OrderType type)
        {
            LINDFINANCIAL_API_COMPONENTLib.IOrder iOrder;
            LINDFINANCIAL_API_COMPONENTLib.ILeg iLeg;

            try
            {
                iOrder = (IOrder)iConnect;
                iLeg = (ILeg)iConnect;
                iOrder.order = "";
                iOrder.recordType = LINDFINANCIAL_API_COMPONENTLib.recordType.RT_ORDER;
                iOrder.firm = Firm ;
                iOrder.account = Account;
                iOrder.firmIdentifier = this.IdGeneration().ToString();  
                iOrder.numberOfLegs = LegIndex.LEG_1;
                
                iLeg.buyOrSell = OrderSharedServices.GetTransactionType(transactionType);
                iLeg.set_quantity(quantity);
                iLeg.month = OrderSharedServices.GetMonth(date);
                iLeg.set_year(OrderSharedServices.GetYear(date));
                iLeg.commodityCode = commodityCode;
                iLeg.OrderType = OrderSharedServices.GetOrderType(type);
                
                if(price > 0)
                    iLeg.set_price(price);

                iOrder.AddLeg(LegIndex.LEG_1, iLeg.leg);
                iOrder.Send();

                Console.WriteLine("{0}", iOrder.order);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void CancelOrder(int orderNumber, string identifier)
        {
            try
            {
                iCancel.cancel = "";
                iCancel.recordType = LINDFINANCIAL_API_COMPONENTLib.recordType.RT_CANCEL;
                iCancel.sequenceNumber = "x";
                iCancel.firm = Firm; 
                iCancel.account = Account; 
                iCancel.firmIdentifier = this.IdGeneration().ToString();
                iCancel.identifier = identifier;
                iCancel.orderNumber = orderNumber;
                iCancel.confirmation = true;
                iCancel.Send();
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }

    }

 

    public enum TransactionType
    {
        Buy,
        Sell,
        None
    }

    public enum OrderType
    {
      MARKET,
      LIMIT,
      STOP,
      STWL,
      STL,
      MIT,
      OB,
      NONE
    }

    public static class OrderSharedServices
    {
        public static LINDFINANCIAL_API_COMPONENTLib.MonthType GetMonth(DateTime date)
        {
            LINDFINANCIAL_API_COMPONENTLib.MonthType month = LINDFINANCIAL_API_COMPONENTLib.MonthType.MT_NONE;
            try
            {
                switch (date.Month)
                {
                    case 1: month = LINDFINANCIAL_API_COMPONENTLib.MonthType.MT_JAN;
                        break;
                    case 2: month = LINDFINANCIAL_API_COMPONENTLib.MonthType.MT_FEB;
                        break;
                    case 3: month = LINDFINANCIAL_API_COMPONENTLib.MonthType.MT_MAR;
                        break;
                    case 4: month = LINDFINANCIAL_API_COMPONENTLib.MonthType.MT_APR;
                        break;
                    case 5: month = LINDFINANCIAL_API_COMPONENTLib.MonthType.MT_MAY;
                        break;
                    case 6: month = LINDFINANCIAL_API_COMPONENTLib.MonthType.MT_JUN;
                        break;
                    case 7: month = LINDFINANCIAL_API_COMPONENTLib.MonthType.MT_JUL;
                        break;
                    case 8: month = LINDFINANCIAL_API_COMPONENTLib.MonthType.MT_AUG;
                        break;
                    case 9: month = LINDFINANCIAL_API_COMPONENTLib.MonthType.MT_SEP;
                        break;
                    case 10: month = LINDFINANCIAL_API_COMPONENTLib.MonthType.MT_OCT;
                        break;
                    case 11: month = LINDFINANCIAL_API_COMPONENTLib.MonthType.MT_NOV;
                        break;
                    case 12: month = LINDFINANCIAL_API_COMPONENTLib.MonthType.MT_DEC;
                        break;
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }

            return month;
        }

        public static int GetYear(DateTime date)
        {
            return date.Year - 2000;
        }

        public static LINDFINANCIAL_API_COMPONENTLib.TransactionType GetTransactionType(TransactionType transaction)
        {
            LINDFINANCIAL_API_COMPONENTLib.TransactionType transactionType = LINDFINANCIAL_API_COMPONENTLib.TransactionType.TT_NONE;
            try
            {
                switch (transaction)
                {
                    case TransactionType.Buy: transactionType = LINDFINANCIAL_API_COMPONENTLib.TransactionType.TT_BUY;
                        break;
                    case TransactionType.Sell: transactionType = LINDFINANCIAL_API_COMPONENTLib.TransactionType.TT_SELL;
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return transactionType;
        }
        public static LINDFINANCIAL_API_COMPONENTLib.OrderType GetOrderType(OrderType type)
        {
            LINDFINANCIAL_API_COMPONENTLib.OrderType orderType =  LINDFINANCIAL_API_COMPONENTLib.OrderType.OT_NONE;   

            try 
	        {	        
          		 switch (type)
                {
                    case OrderType.MARKET: orderType = LINDFINANCIAL_API_COMPONENTLib.OrderType.OT_MARKET;
                         break;
                    case OrderType.LIMIT: orderType = LINDFINANCIAL_API_COMPONENTLib.OrderType.OT_LIMIT;
                         break;
                    case OrderType.MIT: orderType = LINDFINANCIAL_API_COMPONENTLib.OrderType.OT_MIT;
                        break;
                    case OrderType.OB: orderType = LINDFINANCIAL_API_COMPONENTLib.OrderType.OT_OB;
                         break;
                    case OrderType.STL: orderType = LINDFINANCIAL_API_COMPONENTLib.OrderType.OT_STL;
                         break;
                    case OrderType.STOP: orderType = LINDFINANCIAL_API_COMPONENTLib.OrderType.OT_STOP;
                         break;
                    case OrderType.STWL: orderType = LINDFINANCIAL_API_COMPONENTLib.OrderType.OT_STWL;
                         break;
                    default:
                       orderType = LINDFINANCIAL_API_COMPONENTLib.OrderType.OT_NONE;
                         break;
                }
	        }
	        catch (Exception ex)
	        {
		        throw ex;
	        }
            return orderType; 
        }
    
    }

   
}
